var $ = getApp(),methods = require("../methods.js");
Page(Object.assign({}, methods, {
  data: {
		arrLabel: [],
    num: [],
    clickCar: 0,
		cartList:[],
		clist:[],
		numtotal:[],
		index:0,
		gwcnum:1,
  },
  onShow: function() {
		var that = this;
		$.get("shop/fastbuy", { }, function(res) {
				console.log(res);
				that.setData(res);
				//var numtotal = [];
				var arrscroll = [18];
				var s = 18
				for(var i in res.clist){
					s = s + 106 * (res.clist[i].prolist.length ? res.clist[i].prolist.length : .6) + 66;
					arrscroll.push(s)
					//for(var j in res.clist[i].prolist){
					//	numtotal[res.clist[i].prolist[j].id] = 0
					//}
				}
				var arrLab = [1];
				for (var i = 1; i < res.clist.length; i++){
					arrLab.push(0);
				}
				var cartList = res.cartList
				//for(var i in cartList.list){
				//	numtotal[cartList.list[i].proid] += cartList.list[i].num;
				//}
				that.setData({arrLab: arrLab,arrscroll:arrscroll})
		},'加载中');
  },
  clickLab: function(e) {
		var index = e.currentTarget.dataset.index;
		var arrLab = this.data.arrLab;
    for (var i = 0; i < arrLab.length; i++){
			arrLab[i] = 0;
		}
    arrLab[index] = 1; 
		this.setData({
      arrLab: arrLab,
      index: index
    });
  },
  shopCarList: function() {
    var that = this;
    this.setData({
      clickCar: 1,
      cartcartArr: [],
      showPicker: 1
    });
		/*
    $.get("shop/getfastcart", {}, function(res) {
      that.setData({
        cartList: res.cartList
      });
			var tempcartid = [];
      for (var i = 0; i < res.cartList.length; i++){
				tempcartid[i] = res.cartList[i].proid;
			}
      that.setData({
        tempcartid: tempcartid
      });
    });*/
  },
  shopCarHid: function() {
    this.setData({
      clickCar: 0,
      showPicker: 0
    });
  },
  select: function() {
    var t = this;
    i.select(t);
  },
  number: function(t) {
    var a = this;
    i.number(t, a);
  },
  closespecs: function() { 
    this.setData({
      closespecs: 0
    });
  },
  addCartquick: function(num) {
    var that = this;
    $.post("shop/fastupdate", {proid: that.data.product.id,ggid: that.data.ggid,type: that.data.addtype,num: num}, function(res) {
      if (res.status==0){
				$.alert(res.msg);
				that.setData({showPicker: 0});
      }else {
        var cartList = that.data.cartList;
        cartList.total = res.total;
				cartList.totalprice = res.totalprice;
				cartList.list = res.list;
				var numtotal = that.data.numtotal;
				numtotal[that.data.product.id] = res.newnum
				that.setData({
					numtotal: numtotal,
					cartList: cartList,
					showPicker: 0
				});
      }
    });
  },
  cartaddcart: function(e) {
    var that = this;
		var proid = e.currentTarget.dataset.proid
		var ggid = e.currentTarget.dataset.ggid
    var type = e.currentTarget.dataset.type;
		$.post("shop/fastupdate", {proid: proid,ggid: ggid,type: type,num: 1}, function(res) {
			if(res.status==1){
				var cartList = that.data.cartList;
				cartList.total = res.total;
				cartList.totalprice = res.totalprice;
				cartList.list = res.list;
				var numtotal = that.data.numtotal;
				numtotal[proid] = res.newnum;
				that.setData({
					cartList: cartList,
					numtotal: numtotal,
					showoption:false
				});
			}else{
				$.alert(res.msg);
			}
		});
  },
  addGoodToCartFn: function(e) {
    var that = this;
		var clist = that.data.clist
		var type = e.currentTarget.dataset.type;
		var index = e.currentTarget.dataset.index;
		var proidx = e.currentTarget.dataset.proidx;
		var product = clist[index].prolist[proidx]
		that.setData({addtype:type,product:product,ggid:product.guigelist[product.ks].id})
		if(type == 'plus'){ //加
			if (e.currentTarget.dataset.canadd){
				if(product.hasoption){
					that.setData({showoption:true,gwcnum:1})
					console.log(product);
					that.setData({guigelist: product.guigelist,guigedata: product.guigedata,ggselected: product.ggselected,ks: product.ks})
				}else{
					that.animate(e)
					that.addCartquick(1);
				}
			}
		}else{ //减
			if("1" == product.hasoption){
				$.alert("请在购物车中修改多规格商品");
				that.setData({showPicker: 1})
			} else {
				that.addCartquick(1);
			}
		}
  },
		
	buydialogChange: function (e) {
		this.setData({
			showoption: !this.data.showoption
		});
	},
	//选择规格
	ggchange: function (e) {
		var idx = e.currentTarget.dataset.idx;
		var itemk = e.currentTarget.dataset.itemk;
		var ggselected = this.data.ggselected
		ggselected[itemk] = idx
		var ks = ggselected.join(',');
		this.setData({
			ggselected: ggselected,
			ks: ks
		})
	},
	//加入购物车操作
	addcart: function () {
		var that = this;
		var ks = that.data.ks;
		var num = that.data.gwcnum;
		var proid = that.data.product.id;
		var ggid = that.data.guigelist[ks].id;
		$.post('shop/fastupdate', { type:'plus',proid: proid, ggid: ggid, num: num }, function (res) {
			//setTimeout(function () {
				if (res.status == 1) {
					var cartList = that.data.cartList;
					cartList.total = res.total;
					cartList.totalprice = res.totalprice;
					cartList.list = res.list;
					var numtotal = that.data.numtotal;
					numtotal[that.data.product.id] = res.newnum
					that.setData({
						numtotal: numtotal,
						cartList: cartList,
						showoption: 0
					});
				} else {
					$.error(res.msg);
				}
			//}, 300);
		}, '加载中');
	},
	//加
	gwcplus: function (e) {
		var gwcnum = this.data.gwcnum + 1;
		var ks = this.data.ks;
		if (gwcnum > this.data.guigelist[ks].stock) {
			$.error('库存不足');
			return;
		}
		this.setData({
			gwcnum: this.data.gwcnum + 1
		})
	},
	//减
	gwcminus: function (e) {
		var gwcnum = this.data.gwcnum - 1;
		var ks = this.data.ks;
		if (gwcnum <= 0) {
			return;
		}
		this.setData({
			gwcnum: this.data.gwcnum - 1
		})
	},
	//输入
	gwcinput: function (e) {
		var ks = this.data.ks;
		var gwcnum = parseInt(e.detail.value);
		if (gwcnum < 1) return 1;
		if (gwcnum > this.data.guigelist[ks].stock) {
			return this.data.guigelist[ks].stock
		}
		this.setData({
			gwcnum: gwcnum
		});
	},
  animate: function(t) {
		return;
    var that = this;
		that.busPos = {}, that.busPos.x = 30, that.busPos.y = wx.getSystemInfoSync().windowHeight - 40
    that.finger = {};
    var e = {};
    that.finger.x = t.touches[0].clientX;
		that.finger.y = t.touches[0].clientY;
		that.finger.y < that.busPos.y ? e.y = that.finger.y - 150 : e.y = that.busPos.y - 150;
    e.x = Math.abs(that.finger.x - that.busPos.x) / 2;
		that.finger.x > that.busPos.x ? e.x = (that.finger.x - that.busPos.x) / 2 + that.busPos.x : e.x = (that.busPos.x - that.finger.x) / 2 + a.finger.x;
    that.linePos = that.bezier([that.busPos, e,that.finger], 30), that.startAnimation(t);
  },
  bezier: function(t, a) {
    for (var e, r, i, s = [], o = 0; o <= a; o++) {
      for (i = t.slice(0), r = []; e = i.shift();){
        if (i.length){
					r.push(function(t, a) {
						var e, r, i, s, o, n, d, c;
						return e = t[0], r = t[1], s = r.x - e.x, o = r.y - e.y, i = Math.pow(Math.pow(s, 2) + Math.pow(o, 2), .5),
							n = o / s, d = Math.atan(n), c = i * a, {
								x: e.x + c * Math.cos(d),
								y: e.y + c * Math.sin(d)
							};
					}([e, i[0]], o / a));
        }else {
          if (!(r.length > 1)) break;
          i = r, r = [];
        }
			}
      s.push(r[0]);
    }
    return {
      bezier_points: s
    }
  },
  startAnimation: function(t) {
    var a = 0,that = this,r = that.linePos.bezier_points;
    this.setData({
      hide_good_box: !1,
      bus_x: that.finger.x,
      bus_y: that.finger.y
    });
    var i = r.length;
    a = i;
		this.timer = setInterval(function() {
      a--;
			that.setData({
        bus_x: r[a].x,
        bus_y: r[a].y
      });
			if(a < 1){
				clearInterval(that.timer);
				that.setData({
					hide_good_box: !0
				})
			}
    }, 13);
  },
  clearShopCartFn: function() {
    var that = this;
    $.post("shop/fastupdate", { type:'clear' }, function(res) {
      var cartList = {
        list: [],
        total: 0,
        totalprice: 0
      }
			var numtotal = [];
			var clist = that.data.clist
			for(var i in clist){
				for(var j in clist[i].prolist){
					numtotal[clist[i].prolist[j].id] = 0
				}
			}
      that.setData({
        cartList: cartList,
        clickCar: 0,
        numtotal: numtotal,
        clearcart: 0,
        showPicker: 0
      });
    });
  },
  closemulti: function() {
    this.setData({
      showPicker: 0,
      clickCar: 0,
      cycledate: 1
    });
  },
  gopay: function() {
		var cartList = this.data.cartList.list
		if(cartList.length == 0){
			$.alert('请先添加商品到购物车');return ;
		}
		var prodata = [];
		for (var i = 0; i < cartList.length; i++) {
			prodata.push(cartList[i].proid + ',' + cartList[i].ggid + ',' + cartList[i].num);
		}
		$.goto('buy?frompage=fastbuy&prodata=' + prodata.join('-'));
  },
  gotocart: function() {
    $.goto('/pages/shop/cart');
  },
  scrollfn: function(e) {
		var that = this;
		var arrLab = this.data.arrLab;
    for (var i = 0; i < that.data.arrscroll.length; i++){
			arrLab[i] = 0;
      if (Math.abs(e.detail.scrollTop - that.data.arrscroll[i]) < 26) {
        arrLab[i] = 1;
				this.setData({
          arrLab: arrLab
        });
				console.log(arrLab)
        break;
      }
		}
  },
}));